package chances.mock.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import chances.mock.entity.MsgEntity;
import chances.mock.entity.RequestEntity;
import chances.mock.model.BindResponse;
import chances.mock.model.MsgResponse;
import chances.wechat.utils.RequestUtils;

@Controller
@RequestMapping(value = "/mock")
public class MsgBindController {

    @RequestMapping(value = "/bind/mgr")
    public String bind(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {

        String sid = RequestUtils.getParameter(request, "sid", "");
        String cmd = RequestUtils.getParameter(request, "cmd", "");
        String appid = RequestUtils.getParameter(request, "appid", "");
        String iptvacct = RequestUtils.getParameter(request, "iptvacct", "");
        String stbtype = RequestUtils.getParameter(request, "stbtype", "");
        String epgversion = RequestUtils.getParameter(request, "epgversion", "");
        String tradegroup = RequestUtils.getParameter(request, "tradegroup", "");
        String areagroup = RequestUtils.getParameter(request, "areagroup", "");
        String lifetime = RequestUtils.getParameter(request, "lifetime", "");
        String timestamp = RequestUtils.getParameter(request, "timestamp", "");
        String sig = RequestUtils.getParameter(request, "sig", "");
        RequestEntity entity = new RequestEntity(sid, cmd, appid, iptvacct, stbtype, epgversion, tradegroup, areagroup,
                lifetime, timestamp, sig);
        RequestEntity.listRequest.add(entity);
        BindResponse res = new BindResponse();
        request.setAttribute("bind", res);
        //return res.toString();
        return "/bind_mgr";
    }

    @RequestMapping(value = "/push/mgr")
    public String push(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {

        String cmd = RequestUtils.getParameter(request, "cmd", "");
        String sid = RequestUtils.getParameter(request, "sid", "");
        String timestamp = RequestUtils.getParameter(request, "timestamp", "");
        String sig = RequestUtils.getParameter(request, "sig", "");
        String appid = RequestUtils.getParameter(request, "appid", "");
        String starttime = RequestUtils.getParameter(request, "starttime", "");
        int msgtype = RequestUtils.getParameter(request, "msgtype", 0);
        String expire = RequestUtils.getParameter(request, "expire", "");
        String epg = RequestUtils.getParameter(request, "epg", "");
        String receivers = RequestUtils.getParameter(request, "receivers", "");
        String content = RequestUtils.getParameter(request, "content", "");

        long starttimeVal = System.currentTimeMillis();
        if (StringUtils.isNotEmpty(starttime)) {
            starttimeVal = Long.parseLong(starttime);
        }

        long expireVal = System.currentTimeMillis();
        if (StringUtils.isNotEmpty(expire)) {
            expireVal = Long.parseLong(expire);
        }

        long timestampVal = System.currentTimeMillis();
        if (StringUtils.isNotEmpty(timestamp)) {
            timestampVal = Long.parseLong(timestamp);
        }

        MsgEntity msgEntity = new MsgEntity(sid, cmd, appid, starttimeVal, msgtype, expireVal, timestampVal, epg,
                receivers, content, sig);
        MsgEntity.listRequest.add(msgEntity);
        MsgResponse res = new MsgResponse();
        res.setMsgId(19481);
        res.setResCode("0");
        res.setResDesc("成功");
        request.setAttribute("msg", res);
        //return res.toString();
        return "/push_mgr";
    }
    
}
